﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.ServiceModel;
using System.ServiceModel.Channels;

namespace MvcApplicationYoung.Controllers
{
    public class BodyGUIController : Controller
    {
        MvcApplicationYoung.WcfServiceLibrary.IService1 _proxy;

        public BodyGUIController()
        {
            InitWCFService();
        }

        private void InitWCFService()
        {
            //Http绑定
            WSHttpBinding httpBinding = new WSHttpBinding();
            EndpointAddress httpAddr = new EndpointAddress("http://localhost:8888/YoungMVC/Service.svc/test");
            _proxy = new ChannelFactory<MvcApplicationYoung.WcfServiceLibrary.IService1>(httpBinding, httpAddr).CreateChannel();
        }

        private string GetMenues()
        {
            try
            {
                return _proxy.GetMenuesXMLJson();
            }
            catch
            { return ""; }
        }

        private string GetSingleTableJson(string objectID, string objectType)
        {
            try
            {
                return _proxy.GetTableJson(objectID, objectType);
            }
            catch
            { return ""; }
        }

        private string UpdateEmployeeRow(string commitType,string modifyEmployeeRow)
        {
            try
            {
                return _proxy.UpdateEmployeeRow(commitType,modifyEmployeeRow);
            }
            catch
            { return false.ToString(); }
        }

        private string UpdateDepartmentRow(string commitType, string modifyDepartmentRow)
        {
            try
            {
                return _proxy.UpdateDepartmentRow(commitType, modifyDepartmentRow);
            }
            catch
            { return false.ToString(); }
        }

        [HttpGet]
        public ActionResult LoginIn()
        {
            if (Request.IsAuthenticated)
            {
                string jsonString = GetMenues();
                ViewData["menuNodeStr"] = jsonString;
                return View();
            }
            else
            { return RedirectToAction("Index", "Login"); }
        }

        [HttpGet]
        public ActionResult Departments()
        {
            if (Request.IsAuthenticated)
            { return View(); }
            else
            { return null; }
        }
        [HttpGet]
        public ActionResult Employees()
        {
            if (Request.IsAuthenticated)
            { return View(); }
            else
            { return null; }
        }
        [HttpGet]
        public ActionResult Union()
        {
            if (Request.IsAuthenticated)
            { return View(); }
            else
            { return null; }
        }
        [HttpGet]
        public ActionResult ModifyEmployee()
        {
            if (Request.IsAuthenticated)
            { return View(); }
            else
            { return null; }
        }

        [HttpGet]
        public ActionResult ModifyDepartment()
        {
            if (Request.IsAuthenticated)
            { return View(); }
            else
            { return null; }
        }

        public ActionResult LoginOut()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index", "Login");
        }

        [HttpGet]
        public ActionResult GetSingleTable(string objectID, string objectType)
        {
            if (Request.IsAuthenticated)
            {
                //System.Threading.Thread.Sleep(1000);
                return Content(GetSingleTableJson(objectID, objectType)); ;
            }
            else
            { return null; }
        }

        [HttpPost]
        public string UpdateEmployee(string commitType,string modifyEmployee)
        {
            if (Request.IsAuthenticated)
            {
                return UpdateEmployeeRow(commitType,modifyEmployee).ToString();
            }
            else
            { return false.ToString(); }
        }

        [HttpPost]
        public string UpdateDepartment(string commitType, string modifyDepartment)
        {
            if (Request.IsAuthenticated)
            {
                return UpdateDepartmentRow(commitType, modifyDepartment).ToString();
            }
            else
            { return false.ToString(); }
        }

    }
}
